package com.ktgj.train.controller;

import com.alibaba.fastjson.JSONObject;
import com.ktgj.common.core.constant.FlightOrderConstants;
import com.ktgj.common.core.enums.TrainOrderEnum;
import com.ktgj.common.redis.service.RedisService;
import com.ktgj.train.domain.InterfaceEntity.*;
import com.ktgj.train.domain.TrainOrder;
import com.ktgj.train.domain.TrainOrderOperation;
import com.ktgj.train.domain.TrainOrderRefundrecord;
import com.ktgj.train.domain.TrainOrderitem;
import com.ktgj.train.mapper.TrainOrderOperationMapper;
import com.ktgj.train.mapper.TrainOrderRefundrecordMapper;
import com.ktgj.train.service.ITrainOrderRefundrecordService;
import com.ktgj.train.service.ITrainOrderService;
import com.ktgj.train.service.ITrainOrderitemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@ControllerAdvice
@RestController
@RequestMapping("/callback")
public class TrOrderCallbackController {

    @Autowired
    private ITrainOrderService trainOrderService;

    @Autowired
    private ITrainOrderitemService trainOrderitemService;

    @Autowired
    private ITrainOrderRefundrecordService trainOrderRefundrecordService;

    @Autowired
    private RedisService redisService;

    @Autowired
    private TrainOrderRefundrecordMapper trainOrderRefundrecordMapper;

    @Autowired
    private TrainOrderOperationMapper trainOrderOperationMapper;

    /**
     * 占座回调
     * @param request
     */
    @RequestMapping("/trainOrderCallback")
    public void orderCallback(HttpServletResponse response, HttpServletRequest request) {
        String data = request.getParameter("data");
        TrOrderSubmissionsRequest submissionsRequest = JSONObject.parseObject(data, TrOrderSubmissionsRequest.class);
        List<Passengers> list = submissionsRequest.passengers;
        System.out.println("占座回调：" + submissionsRequest.toString() + "乘客信息："+list.toString());
        TrainOrder trainOrder = new TrainOrder();
        Long id = trainOrderService.selectOrderId(submissionsRequest.transactionid);
        trainOrder.setOrderid(id);
        if (submissionsRequest.ordersuccess == true) {
            trainOrder.setOrderpartnernumber(submissionsRequest.transactionid);
            trainOrder.setOrder12306number(submissionsRequest.ordernumber);
            trainOrder.setOrdersuccess(String.valueOf(submissionsRequest.ordersuccess));
            trainOrder.setMsg(submissionsRequest.msg);
            trainOrder.setTicketprice(new BigDecimal(submissionsRequest.orderamount).divide(new BigDecimal(submissionsRequest.passengers.size())));
            trainOrder.setSumamount(new BigDecimal(submissionsRequest.orderamount));
            //trainOrder.setPayamount(new BigDecimal(submissionsRequest.orderamount).add(new BigDecimal(5)));  TODO 测试暂时注掉
            trainOrderService.updateByCallback(trainOrder);

            for (Passengers passenger : submissionsRequest.passengers) {
                Long orderId = trainOrderService.selectOrderId(submissionsRequest.transactionid);
                TrainOrderitem orderItem = new TrainOrderitem();
                orderItem.setOrderid(orderId);
                orderItem.setIdnumber(passenger.passportseno);
                orderItem.setTicketnumber(passenger.ticket_no);
                if (passenger.cxin != null) {
                    String[] cxins = passenger.cxin.split(",");
                    String trainBox = cxins[0];
                    String seatNo = cxins[1];
                    orderItem.setTrainbox(trainBox);
                    orderItem.setSeatno(seatNo);
                }
                orderItem.setTickettype(Long.valueOf(passenger.piaotype));
                orderItem.setPayamount(BigDecimal.valueOf(passenger.price));
                orderItem.setIsQuietCoach(String.valueOf(passenger.isQuietSeat));
                trainOrderitemService.updateByIDNumber(orderItem);
            }

            TrainOrderOperation orderOperation = new TrainOrderOperation();
            orderOperation.setOperationname("占座成功");
            orderOperation.setOrderid(trainOrder.getOrderid());
            orderOperation.setOpemployeeid(1L);
            orderOperation.setOpemployeename("系统服务");
            orderOperation.setCreateTime(new Date());
            trainOrderOperationMapper.insertTrainOrderOperation(orderOperation);
        } else {
            trainOrder.setOrderpartnernumber(submissionsRequest.transactionid);
            trainOrder.setOrdersuccess(String.valueOf(submissionsRequest.ordersuccess));
            trainOrder.setMsg(submissionsRequest.msg);
            trainOrderService.updateByCallback(trainOrder);

            TrainOrderOperation orderOperation = new TrainOrderOperation();
            orderOperation.setOperationname("占座失败");
            orderOperation.setMemo(submissionsRequest.msg);
            orderOperation.setOrderid(trainOrder.getOrderid());
            orderOperation.setOpemployeeid(1L);
            orderOperation.setOpemployeename("系统服务");
            orderOperation.setCreateTime(new Date());
            trainOrderOperationMapper.insertTrainOrderOperation(orderOperation);
        }
        PrintWriter printWriter = null;
        response.setCharacterEncoding("UTF-8");
        try {
            printWriter = response.getWriter();
            printWriter.write("success");
            printWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            redisService.setCacheObject(submissionsRequest.reqtoken, "string");
        }
    }

    /**
     * 确认出票回调
     * @param request
     */
    @RequestMapping("/trainConfirmCallback")
    public void outTicket(HttpServletResponse response, HttpServletRequest request) {
        TrOutTicketsResponse ticketsResponse = new TrOutTicketsResponse();
        ticketsResponse.reqtime = request.getParameter("reqtime");
        ticketsResponse.sign = request.getParameter("sign");
        ticketsResponse.orderid = request.getParameter("orderid");
        ticketsResponse.transactionid = request.getParameter("transactionid");
        ticketsResponse.isSuccess = request.getParameter("isSuccess");
        ticketsResponse.ticket_entrance = request.getParameter("ticket_entrance");
        ticketsResponse.electricticket = request.getParameter("electricticket");
        ticketsResponse.msg = request.getParameter("msg");
        System.out.println("确认出票回调：" + ticketsResponse.toString());
        Long orderId = trainOrderService.selectOrderId(ticketsResponse.transactionid);
        TrainOrder trainOrder = new TrainOrder();
        Long id = trainOrderService.selectOrderId(ticketsResponse.transactionid);
        trainOrder.setOrderid(id);
        TrainOrderitem orderitem = new TrainOrderitem();
        if (ticketsResponse.isSuccess.equals("Y")) {
            trainOrder.setOrderpartnernumber(ticketsResponse.transactionid);
            trainOrder.setStatus(FlightOrderConstants.ticketing.getValue());
            trainOrderService.updateByCallback(trainOrder);

            orderitem.setOrderid(orderId);
            orderitem.setStatus(String.valueOf(FlightOrderConstants.ticketing.getValue()));
            trainOrderitemService.updateByOrderId(orderitem);

            TrainOrderOperation orderOperation = new TrainOrderOperation();
            orderOperation.setOperationname("出票成功");
            orderOperation.setOrderid(trainOrder.getOrderid());
            orderOperation.setOpemployeeid(1L);
            orderOperation.setOpemployeename("系统服务");
            orderOperation.setCreateTime(new Date());
            trainOrderOperationMapper.insertTrainOrderOperation(orderOperation);
        } else {
            trainOrder.setOrderpartnernumber(ticketsResponse.transactionid);
            trainOrder.setStatus(FlightOrderConstants.ticketingFail.getValue());
            trainOrderService.updateByCallback(trainOrder);

            orderitem.setOrderid(orderId);
            orderitem.setStatus(String.valueOf(FlightOrderConstants.ticketingFail.getValue()));
            trainOrderitemService.updateByOrderId(orderitem);

            TrainOrderOperation orderOperation = new TrainOrderOperation();
            orderOperation.setOperationname("出票失败");
            orderOperation.setMemo(ticketsResponse.msg);
            orderOperation.setOrderid(trainOrder.getOrderid());
            orderOperation.setOpemployeeid(1L);
            orderOperation.setOpemployeename("系统服务");
            orderOperation.setCreateTime(new Date());
            trainOrderOperationMapper.insertTrainOrderOperation(orderOperation);
        }

        PrintWriter printWriter = null;
        response.setCharacterEncoding("UTF-8");
        try {
            printWriter = response.getWriter();
            printWriter.write("success");
            printWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 申请退票回调
     * @param request
     */
    @RequestMapping("/returnTicketCallback")
    public void retreatTicket(HttpServletResponse response, HttpServletRequest request) {
        String data = request.getParameter("data");
        TrRetreatTicketsResponse trRetreatTicketsResponse = JSONObject.parseObject(data, TrRetreatTicketsResponse.class);
        List<IsPuTongTui> isPuTongTuis = trRetreatTicketsResponse.isPuTongTui;
        List<IsMoHuTui> isMoHuTuis = trRetreatTicketsResponse.isMoHuTui;
        List<Returntickets> returntickets = trRetreatTicketsResponse.returntickets;
        System.out.println("退票回调：" + trRetreatTicketsResponse + "普通退：" + isPuTongTuis + "模糊退：" + isMoHuTuis + "退票信息：" + returntickets);

        TrainOrderRefundrecord refundrecord = new TrainOrderRefundrecord();
        Long orderId = null;
        for (Returntickets returnticket : trRetreatTicketsResponse.returntickets) {
            TrainOrderitem orderitem = trainOrderitemService.queryOrderitemid(returnticket.ticket_no);
            if (orderId == null)
                orderId = orderitem.getOrderid();
            if (returnticket.returnsuccess == true) {
                //修改子订单信息
                orderitem.setStatus(TrainOrderEnum.refundTicketSuccess.getValue());
                trainOrderitemService.updateTrainOrderitem(orderitem);

                //修改退票记录
                refundrecord.setOrderitemid(orderitem.getOrderitemid());
                refundrecord.setStatus(TrainOrderEnum.refundTicketSuccess.getValue());
                refundrecord.setRefundamount(new BigDecimal(returnticket.returnmoney));
                refundrecord.setChangeType("refund");
                trainOrderRefundrecordService.updateByOrderItemId(refundrecord);

                //新增退票信息
                TrainOrderOperation orderOperation = new TrainOrderOperation();
                orderOperation.setOperationname("退票成功");
                orderOperation.setMemo("乘客：" + orderitem.getGuestname() + "退票成功");
                orderOperation.setOrderid(orderitem.getOrderid());
                orderOperation.setOpemployeeid(1L);
                orderOperation.setOpemployeename("系统服务");
                orderOperation.setCreateTime(new Date());
                trainOrderOperationMapper.insertTrainOrderOperation(orderOperation);
            } else {
                //修改子订单信息
                orderitem.setStatus(TrainOrderEnum.refundTicketFail.getValue());
                trainOrderitemService.updateTrainOrderitem(orderitem);

                //修改退票记录
                refundrecord.setOrderitemid(orderitem.getOrderitemid());
                refundrecord.setStatus(TrainOrderEnum.refundTicketFail.getValue());
                refundrecord.setMemo(returnticket.returnfailmsg);
                refundrecord.setChangeType("refund");
                trainOrderRefundrecordService.updateByOrderItemId(refundrecord);

                //新增退票信息
                TrainOrderOperation orderOperation = new TrainOrderOperation();
                orderOperation.setOperationname("退票失败");
                orderOperation.setMemo("乘客：" + orderitem.getGuestname() + "退票失败");
                orderOperation.setOrderid(orderitem.getOrderid());
                orderOperation.setOpemployeeid(1L);
                orderOperation.setOpemployeename("系统服务");
                orderOperation.setCreateTime(new Date());
                trainOrderOperationMapper.insertTrainOrderOperation(orderOperation);
            }
        }

        if (orderId != null) {
            List<TrainOrderitem> trainOrderitems = trainOrderitemService.selectTrainOrderitemByOrderId(orderId);
            if (trainOrderitems != null && trainOrderitems.size() > 0) {
                long count = trainOrderitems.stream().map(TrainOrderitem::getStatus).distinct().count();
                TrainOrder order = new TrainOrder();
                order.setOrderid(orderId);
                if (count != 1) {
                    order.setStatus(TrainOrderEnum.ticketing.getValue());
                } else {
                    order.setStatus(TrainOrderEnum.refundTicketSuccess.getValue());
                }
                trainOrderService.updateTrainOrder(order);
            }

        }


        PrintWriter printWriter = null;
        response.setCharacterEncoding("UTF-8");
        try {
            printWriter = response.getWriter();
            printWriter.write("success");
            printWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 改签申请退票回调
     * @param request
     */
    @RequestMapping("/returnTicketsCallback")
    public void retreatTickets(HttpServletResponse response, HttpServletRequest request) {
        String data = request.getParameter("data");
        TrRetreatTicketsResponse trRetreatTicketsResponse = JSONObject.parseObject(data, TrRetreatTicketsResponse.class);
        List<IsPuTongTui> isPuTongTuis = trRetreatTicketsResponse.isPuTongTui;
        List<IsMoHuTui> isMoHuTuis = trRetreatTicketsResponse.isMoHuTui;
        List<Returntickets> returntickets = trRetreatTicketsResponse.returntickets;
        System.out.println("退票回调：" + trRetreatTicketsResponse + "普通退：" + isPuTongTuis + "模糊退：" + isMoHuTuis + "退票信息：" + returntickets);

        TrainOrderRefundrecord refundrecord = new TrainOrderRefundrecord();
        for (Returntickets returnticket : trRetreatTicketsResponse.returntickets) {
            TrainOrderRefundrecord orderRefundrecord = trainOrderRefundrecordMapper.queryRecordid(returnticket.ticket_no);
            if (returnticket.returnsuccess) {
                //修改退票记录
                refundrecord.setRecordid(orderRefundrecord.getRecordid());
                refundrecord.setStatus(TrainOrderEnum.refundTicketSuccess.getValue());
                refundrecord.setRefundamount(new BigDecimal(returnticket.returnmoney));
                refundrecord.setOrderStatus(TrainOrderEnum.refundTicketSuccess.getValue());
                trainOrderRefundrecordService.updateTrainOrderRefundrecord(refundrecord);

                //添加操作记录
                TrainOrderOperation orderOperation = new TrainOrderOperation();
                orderOperation.setOperationname("退票成功");
                orderOperation.setMemo("乘客：" + orderRefundrecord.getName() + "退票成功");
                orderOperation.setOrderid(orderRefundrecord.getOrderid());
                orderOperation.setOpemployeeid(1L);
                orderOperation.setOpemployeename("系统服务");
                orderOperation.setCreateTime(new Date());
                trainOrderOperationMapper.insertTrainOrderOperation(orderOperation);
            } else {
                //修改退票记录
                refundrecord.setRecordid(orderRefundrecord.getRecordid());
                refundrecord.setStatus(TrainOrderEnum.refundTicketFail.getValue());
                refundrecord.setMemo(returnticket.returnfailmsg);
                refundrecord.setOrderStatus(TrainOrderEnum.refundTicketFail.getValue());
                trainOrderRefundrecordService.updateTrainOrderRefundrecord(refundrecord);

                //添加操作记录
                TrainOrderOperation orderOperation = new TrainOrderOperation();
                orderOperation.setOperationname("退票失败");
                orderOperation.setMemo("乘客：" + orderRefundrecord.getName() + "退票失败");
                orderOperation.setOrderid(orderRefundrecord.getOrderid());
                orderOperation.setOpemployeeid(1L);
                orderOperation.setOpemployeename("系统服务");
                orderOperation.setCreateTime(new Date());
                trainOrderOperationMapper.insertTrainOrderOperation(orderOperation);
            }
        }

        PrintWriter printWriter = null;
        response.setCharacterEncoding("UTF-8");
        try {
            printWriter = response.getWriter();
            printWriter.write("success");
            printWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 改签占座回调
     * @param response
     * @param request
     */
    @RequestMapping("/trainRequestChangeCallback")
    public void trainRequestChangeCallback(HttpServletResponse response, HttpServletRequest request) {
        String backjson = request.getParameter("backjson");
        TrTicketChangesResponse trTicketChangesResponse = JSONObject.parseObject(backjson, TrTicketChangesResponse.class);
        List<NewTicketCxins> list = trTicketChangesResponse.newtickets;
        System.out.println("改签占座回调：" + trTicketChangesResponse.toString());

        List<TrainOrderRefundrecord> trainOrderRefundrecords = trainOrderRefundrecordMapper.queryTrainOrderRefundrecordByOldTicketNo(trTicketChangesResponse.orderid);
        TrainOrderRefundrecord trainOrderRefundrecord = new TrainOrderRefundrecord();
        if (trTicketChangesResponse.success){//改签成功(是否改签占座成功)
            //trainOrderRefundrecord.setOrderid(Long.valueOf(trTicketChangesResponse.orderid));//主订单编号(商户订单号)
            if (trTicketChangesResponse.priceinfotype == 1){//1：新票款高于原票款(改签票款异动类型code)
                trainOrderRefundrecord.setDifferencePrice(trTicketChangesResponse.pricedifference);//改签差价(差价)
            }
            else if (trTicketChangesResponse.priceinfotype == 2){//2：新票款与原票款相等(改签票款异动类型code)
                trainOrderRefundrecord.setDifferencePrice("0");
            }
            else if (trTicketChangesResponse.priceinfotype == 3) {//3：新票款低于原票款(改签票款异动类型code)
                trainOrderRefundrecord.setDifferencePrice(trTicketChangesResponse.pricedifference);//改签差价(差价)
            }
            //trainOrderRefundrecord.setChangeType(FlightOrderConstants.change.getValue());//改签(枚举)
            //trainOrderRefundrecord.setArrivalStationName(trTicketChangesResponse.to_station_name);//到达车站(到达站名称)
            trainOrderRefundrecord.setMsg(trTicketChangesResponse.msg);//描述信息(提示信息)
            //trainOrderRefundrecord.setMerchantOrderId(trTicketChangesResponse.transactionid);//商户订单号(订单号)
            //trainOrderRefundrecord.setDepartureStationName(trTicketChangesResponse.from_station_name);//出发车站(出发站名称)
            //trainOrderRefundrecord.setTrainNumber(trTicketChangesResponse.checi);//车次号(改签车次)
            //trainOrderRefundrecord.setReqToken(trTicketChangesResponse.reqtoken);//请求特征值(请求特征值)
            trainOrderRefundrecord.setArrivalTime(trTicketChangesResponse.arrive_time);//到达时间(到达时间)
            //trainOrderRefundrecord.setTravelDate(trTicketChangesResponse.train_date);//乘车日期(发车日期)
            //trainOrderRefundrecord.setDepartureTime(trTicketChangesResponse.start_time);//出发时间(发车时间)
            trainOrderRefundrecord.setOrderSuccess(String.valueOf(trTicketChangesResponse.success));//是否占座成功(是否改签占座成功)

            for (NewTicketCxins newTicketCxins : trTicketChangesResponse.newtickets) {
                //trainOrderRefundrecord.setSeatType(newTicketCxins.zwname);//坐席类型(坐席名称)
                trainOrderRefundrecord.setOldTicketNumber(newTicketCxins.old_ticket_no);
                trainOrderRefundrecord.setTicketNumber(newTicketCxins.new_ticket_no);//票号(新车票票号，占座成功会有值，否为"")
                trainOrderRefundrecord.setTicketPrice(new BigDecimal(newTicketCxins.price));//票单价(票价，占座成功会有值，否为"")
                String[] split = newTicketCxins.cxin.split(",");
                trainOrderRefundrecord.setSeatNo(split[1]);//座位号(坐席编码)
                trainOrderRefundrecord.setTrainBox(split[0]);//车厢号(坐席详情，占座成功会有值，否为"")
                trainOrderRefundrecordMapper.updateTrainOrderRefundrecordByOrderId(trainOrderRefundrecord);
            }

            TrainOrderOperation orderOperation = new TrainOrderOperation();
            orderOperation.setOperationname("改签占座成功");
            orderOperation.setOrderid(trainOrderRefundrecords.get(0).getOrderid());
            orderOperation.setOpemployeeid(1L);
            orderOperation.setOpemployeename("系统服务");
            orderOperation.setCreateTime(new Date());
            trainOrderOperationMapper.insertTrainOrderOperation(orderOperation);
        }
        else {
            trainOrderRefundrecord.setMerchantOrderId(trTicketChangesResponse.orderid);//主订单编号(商户订单号)
            trainOrderRefundrecord.setOrderSuccess(String.valueOf(trTicketChangesResponse.success));//是否占座成功(是否改签占座成功)
            trainOrderRefundrecord.setMsg(trTicketChangesResponse.msg);//描述信息(提示信息)
            trainOrderRefundrecordMapper.updateBymerchantOrderId(trainOrderRefundrecord);

            TrainOrderOperation orderOperation = new TrainOrderOperation();
            orderOperation.setOperationname("改签占座失败");
            orderOperation.setMemo(trTicketChangesResponse.msg);
            orderOperation.setOrderid(trainOrderRefundrecords.get(0).getOrderid());
            orderOperation.setOpemployeeid(1L);
            orderOperation.setOpemployeename("系统服务");
            orderOperation.setCreateTime(new Date());
            trainOrderOperationMapper.insertTrainOrderOperation(orderOperation);
        }


        PrintWriter printWriter = null;
        response.setCharacterEncoding("UTF-8");
        try {
            printWriter = response.getWriter();
            printWriter.write("success");
            printWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            redisService.setCacheObject(trTicketChangesResponse.reqtoken, "string");
        }
    }

    /**
     * 确认改签回调
     * @param response
     * @param request
     */
    @RequestMapping("/trainConfirmChangeCallback")
    public void trainConfirmChangeCallback(HttpServletResponse response, HttpServletRequest request) {
        String backjson = request.getParameter("backjson");
        TrTicketConfirmChangeResopnse trTicketConfirmChangeResopnse = JSONObject.parseObject(backjson, TrTicketConfirmChangeResopnse.class);
        List<NewTicketCxins> list = trTicketConfirmChangeResopnse.newticketcxins;
        System.out.println("确认改签回调：" + trTicketConfirmChangeResopnse.toString() + "车票信息：" + list.toString());

        List<TrainOrderRefundrecord> trainOrderRefundrecords = trainOrderRefundrecordMapper.queryTrainOrderRefundrecordByOldTicketNo(trTicketConfirmChangeResopnse.orderid);
        TrainOrderRefundrecord trainOrderRefundrecord = new TrainOrderRefundrecord();
        if (trTicketConfirmChangeResopnse.success) {//确认成功(操作是否成功)
            //trainOrderRefundrecord.setOrderid(Long.valueOf(trTicketConfirmChangeResopnse.orderid));//主订单编号(商户订单号)
            trainOrderRefundrecord.setOrderSuccess(String.valueOf(trTicketConfirmChangeResopnse.success));//是否占座成功(操作是否成功)
            trainOrderRefundrecord.setMsg(trTicketConfirmChangeResopnse.msg);//描述信息()
            if (trTicketConfirmChangeResopnse.ticketpricediffchangeserial != null) {
                //退还票款差价记录的使用方资金变动流水号（改签新票款小于原票款 并且 改签成功才有值 否则为空字符串）
                trainOrderRefundrecord.setTicketpricediffchangeserial(trTicketConfirmChangeResopnse.ticketpricediffchangeserial);
            } else if (trTicketConfirmChangeResopnse.newticketchangeserial != null && trTicketConfirmChangeResopnse.oldticketchangeserial != null) {
                //收取新票票款记录的使用方资金变动流水号（改签新票款大于原票款 并且 改签成功才有值 否则为空字符串）
                trainOrderRefundrecord.setNewticketchangeserial(trTicketConfirmChangeResopnse.newticketchangeserial);
                //收取新票票款记录的使用方资金变动流水号（改签新票款大于原票款 并且 改签成功才有值 否则为空字符串）
                trainOrderRefundrecord.setOldticketchangeserial(trTicketConfirmChangeResopnse.oldticketchangeserial);
            }
            trTicketConfirmChangeResopnse.newticketcxins.forEach(item -> {
                trainOrderRefundrecord.setOldTicketNumber(item.old_ticket_no);
                trainOrderRefundrecord.setOrderStatus(FlightOrderConstants.ticketing.getValue());
                trainOrderRefundrecord.setPaymentOrderStatus(FlightOrderConstants.payd.getValue());
                trainOrderRefundrecordMapper.updateTrainOrderRefundrecordByOrderId(trainOrderRefundrecord);
            });

            TrainOrderOperation orderOperation = new TrainOrderOperation();
            orderOperation.setOperationname("改签出票成功");
            orderOperation.setOrderid(trainOrderRefundrecords.get(0).getOrderid());
            orderOperation.setOpemployeeid(1L);
            orderOperation.setOpemployeename("系统服务");
            orderOperation.setCreateTime(new Date());
            trainOrderOperationMapper.insertTrainOrderOperation(orderOperation);
        } else {
            trTicketConfirmChangeResopnse.newticketcxins.forEach(item -> {
                trainOrderRefundrecord.setMerchantOrderId(trTicketConfirmChangeResopnse.orderid);//主订单编号(商户订单号)
                trainOrderRefundrecord.setOldTicketNumber(item.old_ticket_no);
                trainOrderRefundrecord.setOrderStatus(FlightOrderConstants.ticketing.getValue());
                trainOrderRefundrecord.setPaymentOrderStatus(FlightOrderConstants.payd.getValue());
                trainOrderRefundrecordMapper.updateBymerchantOrderId(trainOrderRefundrecord);
            });
            TrainOrderOperation orderOperation = new TrainOrderOperation();
            orderOperation.setOperationname("改签出票失败");
            orderOperation.setMemo(trTicketConfirmChangeResopnse.msg);
            orderOperation.setOrderid(trainOrderRefundrecords.get(0).getOrderid());
            orderOperation.setOpemployeeid(1L);
            orderOperation.setOpemployeename("系统服务");
            orderOperation.setCreateTime(new Date());
            trainOrderOperationMapper.insertTrainOrderOperation(orderOperation);
        }

        PrintWriter printWriter = null;
        response.setCharacterEncoding("UTF-8");
        try {
            printWriter = response.getWriter();
            printWriter.write("success");
            printWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 退票前置核验回调
     */
    @RequestMapping("/refundPreCheckCallback")
    public void refundPreCheckCallback(HttpServletResponse response, @RequestBody TrTicketRefundPreCheck check) {
        PrintWriter printWriter = null;
        response.setCharacterEncoding("UTF-8");
        try {
            printWriter = response.getWriter();
            printWriter.write("success");
            printWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(check.toString());
    }
}
